// miniprogram/pages/study/study.js
const util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rightoption: '',
    selectedoption: '',
    percent: 0,
    btnText: '下一题',
    rightNum: 0,
    idx: 0,
    length: 0,
    question: {

    },
    errNum: 0,
    rightNum: 0,
    score_arr: [],
    options_arr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let id = options.id;
    this.onQuery(id);
  },
  onQuery: function(id){
    let that = this;
    const db = wx.cloud.database();

    db.collection('history').doc(id)
    .get()
    .then(res => {
      console.log('[数据库] [查询记录] 成功: ', res)
      let questions = res.data.questions;
      that.setData({
        questions,
        length: questions.length
      },()=>{
        that.getCurrentQues(that.data.idx);
      })
    })
  },
  getCurrentQues: function(){
    let question = this.data.questions[this.data.idx];
    this.setData({
      question
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  generate: function(){
    return util.formatTime(new Date());
  },
  goHome: function(){
      let url = '/pages/home/home';
      wx.switchTab({
        url: url
      })
  },
  generate: function(){
    return util.formatTime(new Date());
  },
  doNext: function(){
    console.log('doNext')
    
    let idx = this.data.idx;
    let length = this.data.length;
    idx++;
    
    let percent = ((idx+1)/length)*100;
    if(idx == length){
      this.goHome();
      return;
    }

    if(length-idx == 1){
      this.setData({
        btnText: '完成'
      })
      wx.showToast({
        icon: 'none',
        title: '已经是最后一题了'
      })
    }

    this.setData({
      idx,
      percent
    },()=>{
      this.getCurrentQues(idx);
    })
  }
})